﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace Ellanet
{
    internal class LogonAppsXmlMgmt
    {
        private readonly XmlDocument _logonAppsXmlDoc;

        public enum Action
        {
            Start,
            Stop
        }

        public enum Event
        {
            ServiceStart,
            ServiceStop,
            DisplayLock,
            DisplayUnlock,
            LogOn,
            LogOff
        }

        public LogonAppsXmlMgmt(string xmlPath)
        {
            if (File.Exists(xmlPath))
            {
                _logonAppsXmlDoc = new XmlDocument();
                _logonAppsXmlDoc.Load(xmlPath);
            }
        }

        public string[] GetApplications(Action a, Event e)
        {
            var applications = new List<string>();

            if (_logonAppsXmlDoc != null)
            {
                // ReSharper disable PossibleNullReferenceException
               
                foreach (XmlNode appNode in _logonAppsXmlDoc.SelectNodes(String.Format("logon_screen_launcher/applications/application[{0}/@{1} = \"true\"]", a.ToString().ToLower(), GetXmlAttributeNameForEvent(e))))
                {
                    applications.Add(appNode.Attributes["path"].Value);
                }

                // ReSharper restore PossibleNullReferenceException
            }

            return applications.ToArray();
        }

        public void GetLoggingConfig(out bool enabled, out string path, out int sleepMillisecondsAfterLogOff)
        {
            enabled = false;
            path = null;
            sleepMillisecondsAfterLogOff = 5000;

            if (_logonAppsXmlDoc != null)
            {
                // ReSharper disable PossibleNullReferenceException
               
                try
                {
                    enabled = Convert.ToBoolean(_logonAppsXmlDoc.SelectSingleNode("logon_screen_launcher/logging").Attributes["enabled"].Value);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                try
                {
                    path = _logonAppsXmlDoc.SelectSingleNode("logon_screen_launcher/logging").Attributes["path"].Value;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                try
                {
                    sleepMillisecondsAfterLogOff = Convert.ToInt32(_logonAppsXmlDoc.SelectSingleNode("logon_screen_launcher/options").Attributes["sleep_milliseconds_after_logoff"].Value);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                // ReSharper restore PossibleNullReferenceException
            }
        }

        private string GetXmlAttributeNameForEvent(Event e)
        {
            switch (e)
            {
                case Event.DisplayLock:
                    return "display_lock";
                case Event.DisplayUnlock:
                    return "display_unlock";
                case Event.LogOff:
                    return "logoff";
                case Event.LogOn:
                    return "logon";
                case Event.ServiceStart:
                    return "service_start";
                case Event.ServiceStop:
                    return "service_stop";
                default:
                    return null;
            }
        }
    }
}
